﻿using System;
using System.Collections.Generic;

namespace LazyPlugins.DeclarativeAddinCreator
{

    public enum ButtonVisuals
    {
        StartsNewGroup,
        None
    }
    class MenuConfigItem : LocationConfigItem
    {
        public MenuConfigItem(string name)
            : base(name)
        {
        }
    }
    class ButtonConfigItem : LocationConfigItem
    {
        public ButtonVisuals Visuals { get; set; }

        public ButtonConfigItem(Type buttonType, ButtonVisuals visuals)
            : base(buttonType)
        {
            Visuals = visuals;
        }
    }
    class CommandBarConfigItem : LocationConfigItem
    {
        public CommandBarConfigItem(string name)
            : base(name)
        {
        }
    }

    public class LocationConfigItem
    {
        public LocationConfigItem()
        {
        }

        private readonly bool isButton;

        private readonly Type buttonType;

        public LocationConfigItem(Type buttonType)
        {
            isButton = true;
            this.buttonType = buttonType;
        }

        public Type ButtonType
        {
            get { return buttonType; }
        }

        public LocationConfigItem(string name)
        {
            isButton = false;
            this.name = name;
        }

        public bool IsButton
        {
            get { return isButton; }
        }

        public string Name
        {
            get { return name; }
        }

        public List<LocationConfigItem> Subitems
        {
            get { return subitems; }
        }

        private readonly string name;
        private readonly List<LocationConfigItem> subitems = new List<LocationConfigItem>();
    }
    public class ButtonLocationConfigurator
    {
        private LocationConfigItem currentItem;
        private LocationConfigItem rootItem;

        public ButtonLocationConfigurator UnderCommandBar(string commandbarName)
        {
            if(currentItem != null)
            {
                throw new Exception("Command bar already set eariler");
            }
            var item = new CommandBarConfigItem(commandbarName);
            rootItem = item;
            currentItem = item;
            return this;
        }

        public ButtonLocationConfigurator UnderMenu(string menuName)
        {
            var newItem = new MenuConfigItem(menuName);
            if(currentItem == null)
            {
                throw new Exception("Command bar not initialized yet");
            }
            currentItem.Subitems.Add(newItem);
            currentItem = newItem;
            return this;
        }

        public ButtonLocationConfigurator PlaceButton<T>()
        {
            return PlaceButton<T>(ButtonVisuals.None);
        }

        public ButtonLocationConfigurator PlaceButton<T>(ButtonVisuals visuals)
        {
            if(currentItem == null || !(currentItem is MenuConfigItem) && !(currentItem is CommandBarConfigItem))
            {
                throw new Exception("menu not initialized yet");
            }
            var buttonItem = new ButtonConfigItem(typeof(T), visuals);
            currentItem.Subitems.Add(buttonItem);
            return this;
        }

        public LocationConfigItem RootItem
        {
            get { return rootItem; }
        }
    }
}
